<?php
if (isset($_POST['submit'])) {
  if ($_POST['submit'] == "Add") {
    if (isDateValid($_POST['fav_date'])) {
    $name = trim($_POST['fav_name']);
     $url = trim($_POST['fav_url']);
    $cat = trim($_POST['fav_cat']);
    $date = trim($_POST['fav_date']);
    
    $message="";
    $a = FALSE;
    $b = FALSE;
    //Open Fav file to check allready exit name and Link.
    //$fh = fopen('fav.txt','r');
    //$lines = fopen("data/fav.txt");
    $lines = readData($fav_data);
    foreach ($lines as $key => $val) {
        $line = explode('|', $val);
        $favname = trim($line[1]);
        $link=trim($line[2]);      
       if ($name==$favname) {       
        //add string to error message
         $a = TRUE; 
                
        }
        
        if ($link == $url) {
       $b = True ;
    } 
    } 
    if($a){
    
       $message .=  "<br>ERROR: Fav name already exit please try another Fav name.";
    }
    if($b){
    
       $message .=  "<br>ERROR: URL  already exit please try another URL name.";
    }
    

    //create empty error message       if (strpos($myString, " ") !== false)
    if (empty($name)) {
        //add string to error message
        $message .=  "<br>ERROR: Name cannot be blank";
    }
    if (empty($url)) {
        //add string to error message
        $message .=  "<br>ERROR: Url cannot be blank";
    }
    if (!preg_match("/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i", $url)) {
        $message .= "<br>ERROR: url not recongnize";
    }

    if (!empty($message)) {
        //echo $message ;

        echo '<h4><p id="errormessage">'.$message.'</p></h4>';
    }
    
    if(empty($message)){
        
      addFavourites($name,$url,$cat,$date);
    }
    
      
    }
  } //End of add fav.
  if ($_POST['submit'] == "Del")
    delFavourites($_POST['fav_id']);
}

$cat_lines = readData($cat_data);
?>

<table id="default_table">
  <tr>
    <th>Name</th>
    <th>URL</th>
    <th>Category</th>
    <th>Date</th>
    <th>&nbsp;</th>
  </tr>
  <?php
  $lines = readData($fav_data);

  foreach ($lines as $line) {
    list($fav_id, $fav_name, $fav_url, $fav_cat, $fav_date) = explode("|", $line);
    if ($i2 % 2 > 0) {
      echo '<tr id="row_even">';
    } else {
      echo '<tr id="row_odd">';
    }
    $i2++;
  ?>

    <td><?php echo $fav_name; ?></td>
    <td><a href="<?php echo $fav_url; ?>"><?php echo $fav_url; ?></a></td>
    <td><?php echo $fav_cat; ?></td>
    <td><?php echo $fav_date; ?></td>
    <td><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <input type="hidden" name="fav_id" value="<?php echo $fav_id; ?>" />
        <input type="submit" value="Del" name="submit"/></form>
    </td>
  </tr>
<?php } ?>

  <tr><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <td><input type="text" name="fav_name" /></td>
    <td><input type="text" name="fav_url" value="http://"/></td>
    <td><select name="fav_cat">
      <?php
      foreach ($cat_lines as $cat_line) {
        list(, $cat_name) = explode("|", $cat_line);
        echo "<option>$cat_name</option>\n";
      }
      ?>
    </select></td>
  <td><input type="text" name="fav_date" value="<?php echo date("Y-m-d"); ?>" /></td>
  <td><input type="submit" value="Add" name="submit" /></td>
</form></tr>
</table>
